package com.drinks.yiniang.adapter.community;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.comment.CommentJson;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 社区评论adapter
 * @author han
 * @date 2019/8/30
 */
public class CommunityCommentsAdapter extends BaseQuickRCVAdapter<CommentJson.CommentList, BaseViewHolder> {

    private SpannableStringBuilder mBuilder;

    private int startName, endName, startReply, endReply;
    private int userNameColor, mReplyColor;

    public CommunityCommentsAdapter(Context mContext, @Nullable List<CommentJson.CommentList> data) {
        super(R.layout.item_community_comments, data);

        mBuilder = new SpannableStringBuilder();

        userNameColor = ColorUtils.getColorById(mContext, R.color.color_282829);
        mReplyColor = ColorUtils.getColorById(mContext, R.color.color_b4b4b4);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentJson.CommentList item) {
        mBuilder.clearSpans();
        mBuilder.clear();
        startName = 0;
        if(StringUtils.isPositiveIntNumber(item.getPid())) {
            mBuilder.append(StringUtils.getNoNullString(item.getNickname()));
            mBuilder.append("@");
            mBuilder.append(StringUtils.getNoNullString(item.getAt_fans()));
        } else {
            mBuilder.append(StringUtils.getNoNullString(item.getNickname()));
        }
        endName = mBuilder.length();
        mBuilder.append("  ");
        mBuilder.append(StringUtils.getNoNullString(item.getContent()));
        mBuilder.append("  ");
        startReply = mBuilder.length();
        mBuilder.append("回复");
        endReply = mBuilder.length();

        mBuilder.setSpan(new ForegroundColorSpan(userNameColor), startName, endName, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBuilder.setSpan(new StyleSpan(Typeface.BOLD), startName, endName, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBuilder.setSpan(new ForegroundColorSpan(mReplyColor), startReply, endReply, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        helper.setText(R.id.tv_c_comments_content, mBuilder);
        //时间
        helper.setText(R.id.tv_c_comments_time, item.getCreated_at());
    }
}
